from woniuatm_2.utility import *
import random

login_index = -1

# 注册
def reg():
    # username = input("请输入用户名[注册]：")
    # password = input('请输入密码[注册]：')
    # phone = input('请输入电话号码[注册]：')
    #
    # if check_user(username) >= 0:
    #     print("你注册的用户名已经存在.")
    #     reg()
    # elif not check_phone(phone):
    #     print("你注册的电话号码不正确.")
    #     reg()
    # else:
    #     print("恭喜你，用户名可用.")
    #     user_info = ''
    #     user_info += username + ','
    #     user_info += password + ','
    #     user_info += str(random.randrange(1000, 5000, 100)) + ','
    #     user_info += phone + '\n'
    #
    #     write_userinfo(user_info)
    #     print("恭喜你，注册成功.")
    #     from woniuatm_2.menu import start_menu
    #     start_menu()

    username = input_username()
    password = input_password()
    phone = input_phone()
    user_info = ''
    user_info += username + ','
    user_info += password + ','
    user_info += str(random.randrange(1000, 5000, 100)) + ','
    user_info += phone + '\n'

    write_userinfo(user_info)
    print("恭喜你，注册成功.")
    from woniuatm_2.menu import start_menu
    start_menu()


def input_username():
    username = input("请输入用户名[注册]：")
    if check_user(username) >= 0:
        print("你注册的用户名已经存在.")
        input_username()
    else:
        return username

def input_password():
    password = input('请输入密码[注册]：')
    if len(password) < 5:
        print("你输入的密码过短.")
        input_password()
    else:
        return password

def input_phone():
    phone = input('请输入电话号码[注册]：')
    if not check_phone(phone):
        print("你注册的电话号码不正确.")
        input_phone()
    else:
        return phone

# 登录
def login():
    username = input("请输入用户名[登录]：")
    password = input('请输入密码[登录]：')

    index = check_user(username)
    if index >= 0:
        user_list = read_user()
        if user_list[index].split(',')[1] == password:
            print("恭喜你，登录成功.")
            global login_index  # 要在函数中修改全局变量，则必须加global进行声明，如果不修改只读取，只可以直接读取
            login_index = index  # 修改全局变量login_index为当前登录用户的下标
            from woniuatm_2.menu import main_menu
            main_menu()
        else:
            print("抱歉，密码输入错误.")
    else:
        print("抱歉，登录用户名不存在.")

# 检查用户信息并返回下标
def check_user(username):
    user_list = read_user()
    for i in range(1, len(user_list)):
        if user_list[i].split(',')[0] == username:
            return i

    return -1


username = input_username()
print(username)